import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    login: true,
    ismale: "male", // 男生male女生female
    lately: [], // 最近阅读
    seahistory: [], // 搜索历史
    shelf: [], // 书架
    skin: {
      "daytime": {
        // 日间
        color: 'rgba(0, 0, 0, 0.4)', // 标题颜色 页数颜色
        background: 'url("/back.jpg") no-repeat center top/100%,url("/back.jpg") no-repeat center bottom/100%,#c4b395',
        conColor: 'rgba(0, 0, 0, 0.8)', // 内容颜色
      },
      "atnight": {
        // 夜间
        color: 'rgba(255, 255, 255, 0.5)',
        background: ' #1a1a1a',
        conColor: 'rgba(255, 255, 255, 0.5)'
      }
    }, // 肤色
    curSkin: 'daytime', // 当前肤色
    size: 48, // 阅读界面字体 百分比
  },
  mutations: {
    setIsmale(state, flag) {
      state.ismale = flag;
    },
    setLately(state, obj) {
      let la = state.lately;
      la.some((item, index) => {
        if (!item._id) {
          return true
        }
        if (item._id == obj._id) {
          la.splice(index, 1);
          return true;
        }
      })
      la.unshift(obj);
      state.lately = la;
    },
    setSeaHistory(state, {
      word,
      flag
    }) {
      // 清空
      if (flag) {
        if (state.seahistory.length > 9) {
          state.seahistory.unshift(word);
          state.seahistory.pop();
        } else {
          let index = state.seahistory.findIndex(item => item == word);
          if (index != -1) {
            state.seahistory.splice(index, 1);
          }
          state.seahistory.unshift(word);
        }
      } else {
        state.seahistory = [];
      }
    },
    setShelf(state, obj) {
      let s = state.shelf;
      s.some((item, index) => {
        if (!item._id) {
          // 空时返回
          return true
        }
        if (item._id == obj._id) {
          // 数据里面有相同时删除，重新赋值到数据最前面
          s.splice(index, 1);
          return true;
        }
      })
      s.unshift(obj);
      state.shelf = s;
    },
    delShelf(state, obj) {
      // 删除书架内容 obj是一个要删除的数据 数组类型
      let shelf = state.shelf;
      obj.forEach((item, index) => {
        shelf.forEach((k) => {
          if (k._id == item._id) {
            state.shelf.splice(index, 1);
          }
        });
      });

    },
    setSkin(state, skin) {
      state.curSkin = skin;
    },
    setSize(state, size) {
      state.size = size;
    },
    setbackC(state, color) {
      state.skin.daytime.background =  color;
    }
  },
  actions: {},
  modules: {}
})